package capricornus.net.http
{
	import capricornus.events.LoaderEvent;
	import capricornus.net.loader.SingleUrlLoader;
	
	import flash.net.URLLoaderDataFormat;

	internal class HttpRequest
	{
		private var _id:int;
		private var _url:String;
		private var _callBackFun:Function;
		
		private var _loader:SingleUrlLoader;
		
		public function HttpRequest(id:int, url:String, callBackFun:Function)
		{
			_id = id;
			_url = url;
			_callBackFun = callBackFun;
			init();
		}
		
		private function init():void
		{
			_loader = new SingleUrlLoader(URLLoaderDataFormat.TEXT);
			_loader.addEventListener( LoaderEvent.LOAD_PROGRESS, onLoadProgress );
			_loader.addEventListener( LoaderEvent.LOAD_COMPLETE, onLoadedComplete );
			_loader.load( _url );
		}
		
		public function destroy():void
		{
			if( _loader != null )
			{
				_loader.removeEventListener( LoaderEvent.LOAD_COMPLETE, onLoadedComplete );
				_loader.destroy();
				_loader = null;
			}
		}
		
		private function onLoadProgress( e:LoaderEvent ):void
		{
			
		}
		
		private function onLoadedComplete( e:LoaderEvent ):void
		{
			var str:String = e.data as String;
			if( str != null )
			{
				if( _callBackFun != null )
				{
					_callBackFun( str );
				}
			}
			HttpRequestManager.clear( _id );
		}
	}
}